<template>
  <div>
    <Table
      ref="dataTable"
      stripe
      :loading="table.loading"
      :columns="table.tableColumns"
      :data="table.tableRows"
      style="margin-top:20px;"
      @on-select="select" @on-select-cancel="selectCancel"
               @on-select-all="selectAll" @on-select-all-cancel="selectAllCancel"
      @on-sort-change="changeSort"
    ></Table>
    <div style="margin: 20px; overflow: hidden">
      <div style="float: right;">
        <Page
          :total="pager.total"
          :current="pager.pageNo"
          @on-change="changePageNo"
          @on-page-size-change="changePageSize"
          showSizer
          showTotal
        ></Page>
      </div>
    </div>
  </div>
</template>

<script>
import * as utils from '@/api/utils-v2'
import * as ResponseStatus from '@/api/response-status'

export default {
  name: 'GoodsAttributeTableMain',
  data() {
    return {
      urls: {
        searchUrl: '/goods-attribute/admin/pager-cond',
        activeUrl: '/goods-attribute/admin/active',
        removeUrl: '/goods-attribute/admin/remove/',
        updateStatusUrl: '/goods-attribute/admin/update-status'
      },
      pager: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      },
	  dataDictCodes: ['IS_ACTIVE'],
	  dataDictItems: {},
      searchOpts: {
        sortColumn: null,
        sortOrder: null
      },
      table: {
        loading: false,
        tableColumns: [
          {
            type: 'selection',
            width: 52,
            key: 'id',
            align: 'center',
            fixed: 'left'
          },
          {
            width: 60,
            align: 'center',
            fixed: 'left',
            render: (h, params) => {
              return h(
                'span',
                params.index + (this.pager.pageNo - 1) * this.pager.pageSize + 1
              )
            }
          },
          {
            title: '商品属性编号',
            key: 'id',
            minWidth: 120,
            sortable: true
          },
          {
            title: '属性名称',
            key: 'attrName',
            minWidth: 120,
            sortable: true
          },
          {
            title: '属性代码',
            key: 'attrCode',
            minWidth: 120,
            sortable: true
          },
          {
            title: '数据类型',
            key: 'attrType',
            minWidth: 120,
            sortable: true
          },
          {
            title: '数据长度',
            key: 'attrLength',
            minWidth: 120,
            sortable: true
          },
          {
            title: '是否必填',
            key: 'attrRequired',
            minWidth: 120,
            sortable: true,
            render: (h, params) => {
              if (params.row.attrRequired === 0) {
                return h('Tooltip', {
                  props: {
                    content: '点击非必填',
                    placement: 'top',
                    transfer: true,
                    maxWidth: 500
                  }
                }, [
                  h('Button', {
                    props: {
                      type: 'success',
                      size: 'small'
                    },
                    on: {
                      'click': () => {
                        this.updateStatus(params.row, 0)
                      }
                    }
                  }, '必填')
                ])
              } else {
                return h('Tooltip', {
                  props: {
                    content: '点击必填',
                    placement: 'top',
                    transfer: true,
                    maxWidth: 500
                  }
                }, [
                  h('Button', {
                    props: {
                      type: 'warning',
                      size: 'small'
                    },
                    on: {
                      'click': () => {
                        this.updateStatus(params.row, 0)
                      }
                    }
                  }, '非必填')
                ])
              }
            }
          },
          {
            title: '是否前端显示',
            key: 'attrDisplay',
            minWidth: 120,
            sortable: true,
            render: (h, params) => {
              if (params.row.attrDisplay === 0) {
                return h('Tooltip', {
                  props: {
                    content: '点击非前端显示',
                    placement: 'top',
                    transfer: true,
                    maxWidth: 500
                  }
                }, [
                  h('Button', {
                    props: {
                      type: 'success',
                      size: 'small'
                    },
                    on: {
                      'click': () => {
                        this.updateStatus(params.row, 1)
                      }
                    }
                  }, '显示')
                ])
              } else {
                return h('Tooltip', {
                  props: {
                    content: '点击前端显示',
                    placement: 'top',
                    transfer: true,
                    maxWidth: 500
                  }
                }, [
                  h('Button', {
                    props: {
                      type: 'warning',
                      size: 'small'
                    },
                    on: {
                      'click': () => {
                        this.updateStatus(params.row, 1)
                      }
                    }
                  }, '不显示')
                ])
              }
            }
          },
          {
            title: '版本号',
            key: 'version',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '版本号'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '商品属性版本号',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            }
          },
          {
            title: '创建时间',
            key: 'createTime',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '创建时间'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '商品属性创建时间',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            }
          },
          {
            title: '更新时间',
            key: 'updateTime',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '更新时间'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '商品属性更新时间',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            }
          },
          {
            title: '是否激活',
            key: 'isActive',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '是否激活'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '商品属性是否激活',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            },
			render: (h, params) => {
							let dictItem = utils.getDataDictItemDisplayValue(this, 'IS_ACTIVE', params.row.isActive)
							return h('span', dictItem.displayValue)
			 }
          },

          {
            title: '激活状态',
            key: 'isActive',
            minWidth: 100,
            align: 'center',
            render: (h, params) => {
              if (params.row.isActive === 0) {
                return h('Tooltip', {
                  props: {
                    content: '点击冻结',
                    placement: 'top',
                    transfer: true,
                    maxWidth: 500
                  }
                }, [
                  h('Button', {
                    props: {
                      type: 'success',
                      size: 'small'
                    },
                    on: {
                      'click': () => {
                        this.active(params.row)
                      }
                    }
                  }, '激活')
                ])
              } else {
                return h('Tooltip', {
                  props: {
                    content: '点击激活',
                    placement: 'top',
                    transfer: true,
                    maxWidth: 500
                  }
                }, [
                  h('Button', {
                    props: {
                      type: 'error',
                      size: 'small'
                    },
                    on: {
                      'click': () => {
                        this.active(params.row)
                      }
                    }
                  }, '冻结')
                ])
              }
            }
          },
          {
            title: '操作',
            key: 'action',
            width: 130,
            align: 'center',
            fixed: 'right',
            render: (h, params) => {
              return h(
                'Dropdown',
                {
                  on: {
                    'on-click': itemName => {
                      this.userOpt(itemName, params.row)
                    }
                  },
                  props: {
                    transfer: true
                  }
                },
                [
                  h(
                    'Button',
                    {
                      props: {
                        type: 'primary',
                        size: 'small'
                      }
                    },
                    [
                      '选择操作 ',
                      h('Icon', {
                        props: {
                          type: 'ios-arrow-down'
                        }
                      })
                    ]
                  ),
                  h(
                    'DropdownMenu',
                    {
                      slot: 'list'
                    },
                    [
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'showEdit'
                          }
                        },
                        '编辑'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'showDetail'
                          }
                        },
                        '详情'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'remove'
                          }
                        },
                        [
                          h(
                            'span',
                            {
                              style: {
                                color: 'red'
                              }
                            },
                            '删除'
                          )
                        ]
                      )
                    ]
                  )
                ]
              )
            }
          }
        ],
        tableRows: [],
        selections: []
      }
    }
  },
  computed: {},
  mounted() {
    this.search()
  },
  methods: {
    search() {
      this.$emit('searchTable')
    },
    userOpt(itemName, row) {
      if (itemName === 'showEdit') {
        this.$emit('showEditModal', JSON.parse(JSON.stringify(row)))
      } else if (itemName === 'showDetail') {
        this.$emit('showDetailModal', JSON.parse(JSON.stringify(row)))
      } else if (itemName === 'remove') {
        utils.remove(this, row)
      }
    },
    active(row) {
      utils.active(this, row)
    },
    select(selections, row) {
              utils.select(this, selections, row)
            },
            selectCancel(selections, row) {
              utils.selectCancel(this, selections, row)
            },
            selectAll(selections) {
              utils.selectAll(this, selections)
            },
            selectAllCancel(selections) {
              utils.selectAllCancel(this, selections)
            },
    changeSort(sortColumn) {
      utils.changeSort(this, sortColumn)
    },
    changePageNo(pageNo) {
      utils.changePageNo(this, pageNo)
    },
    changePageSize(pageSize) {
      utils.changePageSize(this, pageSize)
    },
    /**
     * 更新是否必填和是否显示的状态
     */
    updateStatus(row, type) {
      // 默认是是否必填
      let status = row.attrRequired
      if (type === 1) {
        // 更新是否显示
        status = row.attrDisplay
      }
      status = status === 0 ? 1 : 0
      let param = {
        id: row.id,
        attrRequired: status
      }
      if (type === 1) {
        param = {
          id: row.id,
          attrDisplay: status
        }
      }
      utils.doPostJson(this.urls.updateStatusUrl, param, {}).then(res => {
        if (ResponseStatus.OK === res.data.code) {
          this.$Message.success(res.data.message)
          this.search()
        } else {
          this.$Message.error(res.data.message)
        }
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style>
</style>
